#!/bin/sh

MAKEWHAT=
EXTRAFLAGS=
STRIPFLAG=-s
isrelease=n
skipcompile=n
if test $# -ge 1 ; then
  Platname=$1
  shift
  while test $# -ge 1 
  do
    if test "$1" = "-release" ; then
      isrelease=y
    elif test "$1" = "-skipcompile" ; then
      skipcompile=y
    elif test "$1" = "-tecio" ; then
      MAKEWHAT=tecio.a
    else
      EXTRAFLAGS="$EXTRAFLAGS $1"
    fi
    shift
  done
else
  echo "Choose platform:"
  echo " "
  echo "  macx.101"
  echo "  umacx.101"
  echo "  sgix.62"
  echo "  sgix3.62"
  echo "  sgix.65"
  echo "  sgix64.65"
  echo "  sgix2.65"
  echo "  sun4.57"
  echo "  sun464.57"
  echo "  sun86.54"
  echo "  ibmx.43"
  echo "  ibmx64.43"
  echo "  ibmx.51"
  echo "  ibmx64.51"
  echo "  decalpha.32"
  echo "  compaq.51"
  echo "  hp7xx.11"
  echo "  hp7xx64.11"
  echo "  linux.24"
  echo "  linuxi64.24"
  echo "  linux.22"
  echo "  linuxa.22"
  echo "  crayc90"
  echo "->\c"
  read Platname

  echo "Choose:"
  echo "  1.  Make tecio.a only"
  echo "  2.  Make tecio.a and pltview"

  read choice

  if test $choice -eq 1 ;then 
    MAKEWHAT=tecio.a
  fi
fi

MAKECMD=make
LINKFLAGS=
LINKLIBS=
AR=ar
ARFLAGS=qv
DISTSUBDIR2=

case $Platname in
   macix*-64)  CCOMP=g++
               FINALCFLAGS="-m64 -DDARWIN -DLONGIS64 -I/usr/X11R6/include"
               STRIPFLAG=-Wl,-x
               ;;
   macix.104)  CCOMP=g++
               FINALCFLAGS="-DDARWIN -I/usr/X11R6/include"
               STRIPFLAG=-Wl,-x
               ;;
   *macx*)     CCOMP=CC
               FINALCFLAGS="-DDARWIN -no-cpp-precomp -I/usr/X11R6/include"
               AR="/usr/bin/libtool"
               ARFLAGS="-static -o"
               LINKLIBS=/usr/lib/libstdc++.a
               STRIPFLAG=-Wl,-x
               ;;
   sgix.65-64) CCOMP=CC
               FINALCFLAGS="-DIRISX -DLONGIS64 -mips4 -64"
               LINKFLAGS="-mips4 -64"
               ;;
   sgix64.65)  CCOMP=CC
               FINALCFLAGS="-DIRISX -DLONGIS64 -mips4 -64"
               LINKFLAGS="-mips4 -64"
               ;;
   sgix.65)    CCOMP=CC
               FINALCFLAGS="-DIRISX -mips4 -n32"
               LINKFLAGS="-mips4 -n32"
               ;;
   sgix2.65)   CCOMP=CC
               FINALCFLAGS="-DIRISX -o32"
               LINKFLAGS="-o32"
               ;;
   sgix.62-64) CCOMP=CC
               FINALCFLAGS="-DIRISX -DIRIX62 -DLONGIS64 -mips4 -64"
               LINKFLAGS="-mips4 -64"
               ;;
   sgix.62)    CCOMP=CC
               FINALCFLAGS="-DIRISX -DIRIX62 -mips4 -n32"
               LINKFLAGS="-mips4 -n32"
               ;;
   sgix1.62)   CCOMP=CC
               FINALCFLAGS="-DIRISX -DIRIX62 -mips1 -32"
               LINKFLAGS="-mips1 -32"
               ;;
   sgix3.62)   CCOMP=CC
               FINALCFLAGS="-DIRISX -DIRIX62 -mips3 -n32"
               LINKFLAGS="-mips3 -n32"
               ;;
   ibmx.*)     CCOMP=xlC
               FINALCFLAGS=-DIBMRS6000X
               ;;
   ibmx64.*)   CCOMP=xlC
               FINALCFLAGS="-DIBMRS6000X -DLONGIS64 -q64"
               ARFLAGS="-X64 qv"
               ;;
   compaq.51)  CCOMP=cxx
               FINALCFLAGS="-DCOMPAQX -I/usr/include -ieee_with_inexact" 
               ;;
   decalpha.32)CCOMP=cc
               FINALCFLAGS="-DDECALPHAX -I/usr/include -ieee_with_inexact" 
               ;;
   hp7xx.*-64) CCOMP=aCC
               FINALCFLAGS="+DD64 +DS2.0 -AA -DHPX -DLONGIS64 -I/usr/include/X11R6 -I/usr/include/Motif2.1" 
               LINKFLAGS="+DA2.0W +DD64 +DS2.0W"
               ;;
   hp7xx64.11) CCOMP=aCC
               FINALCFLAGS="+DA2.0W +DD64 +DS2.0W -AA -DHPX -DLONGIS64 -I/usr/include/X11R6 -I/usr/include/Motif2.1" 
               LINKFLAGS="+DA2.0W +DD64 +DS2.0W"
               ;;
   hp7xx.11)   CCOMP=aCC
               FINALCFLAGS="+DAportable -AA -DHPX -I/usr/include/X11R6 -I/usr/include/Motif2.1" 
               LINKFLAGS="+DAportable"
               ;;
   crayc90)    CCOMP=cc
               FINALCFLAGS="-DCRAY -DUNIXX" 
               ;;
   linux*i64.*)CCOMP=g++
               FINALCFLAGS="-fPIC -DLINUX -DLINUXI64" 
               ;;
   linux*64.*) CCOMP=g++
               FINALCFLAGS="-fPIC -DLINUX -DLINUX64" 
               ;;
   linux*)     CCOMP=g++
               FINALCFLAGS="-fPIC -DLINUX" 
               ;;
   sun4.54)    CCOMP=/opt/SUNWspro/bin/CC
               FINALCFLAGS="-DSUN -DSUNSOLARISX -I/usr/openwin/include -I/usr/dt/include -library=libC -library=Cstd -library=no%rwtools7 -library=no%rwtools7_dbg -xO1"
               LINKFLAGS="-library=libC -library=Cstd -library=no%rwtools7 -library=no%rwtools7_dbg"
               MAKECMD=/usr/ccs/bin/make
               AR=/opt/SUNWspro/bin/CC
               ARFLAGS="-xar -o"
               ;;
   sun4.55)    CCOMP=/opt/SUNWspro/bin/CC
               FINALCFLAGS="-DSUN -DSUNSOLARISX -I/usr/openwin/include -I/usr/dt/include -library=libC -library=Cstd -library=no%rwtools7 -library=no%rwtools7_dbg -xO1"
               LINKFLAGS="-library=libC -library=Cstd -library=no%rwtools7 -library=no%rwtools7_dbg"
               MAKECMD=/usr/ccs/bin/make
               AR=/opt/SUNWspro/bin/CC
               ARFLAGS="-xar -o"
               ;;
   sun4.57)    CCOMP=/opt/SUNWspro/bin/CC
               FINALCFLAGS="-DSUNSOLARISX -I/usr/openwin/include -I/usr/dt/include -library=libC -library=Cstd -library=no%rwtools7 -library=no%rwtools7_dbg -xO1"
               LINKFLAGS="-library=libC -library=Cstd -library=no%rwtools7 -library=no%rwtools7_dbg"
               MAKECMD=/usr/ccs/bin/make
               AR=/opt/SUNWspro/bin/CC
               ARFLAGS="-xar -o"
               ;;
   sun464.57)  CCOMP=/opt/SUNWspro/bin/CC
               FINALCFLAGS="-DSUNSOLARISX -DLONGIS64 -KPIC -xarch=v9 -I/usr/openwin/include -I/usr/dt/include -library=libC -library=Cstd -library=no%rwtools7 -library=no%rwtools7_dbg -xO1"
               LINKFLAGS="-KPIC -xarch=v9 -library=libC -library=Cstd -library=no%rwtools7 -library=no%rwtools7_dbg"
               MAKECMD=/usr/ccs/bin/make
               AR=/opt/SUNWspro/bin/CC
               ARFLAGS="-xar -o"
               ;;
   sun4.57-64) CCOMP=/opt/SUNWspro/bin/CC
               FINALCFLAGS="-DSUNSOLARISX -DLONGIS64 -KPIC -xarch=v9 -I/usr/openwin/include -I/usr/dt/include -library=libC -library=Cstd -library=no%rwtools7 -library=no%rwtools7_dbg -xO1"
               LINKFLAGS="-KPIC -xarch=v9 -library=libC -library=Cstd -library=no%rwtools7 -library=no%rwtools7_dbg"
               MAKECMD=/usr/ccs/bin/make
               AR=/opt/SUNWspro/bin/CC
               ARFLAGS="-xar -o"
               ;;
   sun86.54)   CCOMP=/opt/SUNWspro/bin/CC
               FINALCFLAGS="-DSUN -DSUNSOLARISX -I/usr/openwin/include -I/usr/dt/include" 
               MAKECMD=/usr/ccs/bin/make
               AR=/opt/SUNWspro/bin/CC
               ARFLAGS="-xar -o"
               ;;
   *)          echo "Err: Invalid platform"
               exit
               ;;
esac

if test "$isrelease" = "y" ; then
  EXTRAFLAGS="$EXTRAFLAGS -DNO_ASSERTS"
else
  STRIPFLAG=
fi

if test "$skipcompile" = "n" ; then
  rm -f *.o */*.o *.a > /dev/null 2>&1
fi

rm -f *.a > /dev/null 2>&1



FINALCFLAGS="$FINALCFLAGS $EXTRAFLAGS -DUSEENUM -DTHREED"
#
# NOTE: Used to use make here but had problems with using remsh to run
#       make multiple times to get 64 bit and 32 bit versions of libraries....
#
# $MAKECMD $MAKEWHAT AR=$AR CC=$CCOMP LINKFLAGS="$LINKFLAGS" STRIPFLAG=$STRIPFLAG CFLAGS="$EXTRAFLAGS -DUSEENUM -DTHREED $FINALCFLAGS" 
#
#
#

cd tecsrc

BASELIST=`/bin/ls -1 *.cpp`

OBJLIST=
for file in $BASELIST
do
  OBJNAME=`echo $file | sed 's/\.cpp/.o/'`
  OBJLIST="$OBJLIST tecsrc/$OBJNAME"
done



if test "$skipcompile" = "n" ; then
  for file in $BASELIST
  do
    case $file in
      tecxxx.cpp)   ARCHIVEFLAG=-DMAKEARCHIVE;;
      arrlist.cpp)  ARCHIVEFLAG=-DMAKEARCHIVE;;
      datautil.cpp) ARCHIVEFLAG=-DMAKEARCHIVE;;
      *)            ARCHIVEFLAG= ;;
    esac
    echo "$CCOMP $FINALCFLAGS $ARCHIVEFLAG -c $file"
    $CCOMP $FINALCFLAGS $ARCHIVEFLAG -c $file
  done
fi

cd ..

pwd


echo "$AR $ARFLAGS tecio.a $OBJLIST"
$AR $ARFLAGS tecio.a $OBJLIST
if test  -f /bin/ranlib ; then 
    /bin/ranlib tecio.a; 
elif test -f /usr/bin/ranlib ; then 
    /usr/bin/ranlib tecio.a; 
elif test -f /usr/ucb/ranlib ; then 
    /usr/ucb/ranlib tecio.a; 
fi

echo "$CCOMP -I./tecsrc -DMAKEARCHIVE $FINALCFLAGS -c pltview.cpp"
$CCOMP -I./tecsrc -DMAKEARCHIVE $FINALCFLAGS -c pltview.cpp

echo "$CCOMP $FINALCFLAGS pltview.o tecio.a $LINKFLAGS $LINKLIBS $STRIPFLAG -lm -o pltview"
$CCOMP $FINALCFLAGS pltview.o tecio.a $LINKFLAGS $LINKLIBS $STRIPFLAG -lm -o pltview
